package com.liujit.upms.controller;

import com.liujit.common.constants.GlobalConstant;
import com.liujit.common.domain.params.BaseParams;
import com.liujit.common.domain.result.PageBean;
import com.liujit.upms.domain.model.SysUser;
import com.liujit.upms.domain.params.CheckPwdParams;
import com.liujit.upms.domain.params.ModifyPwdParams;
import com.liujit.upms.domain.params.SysUserParams;
import com.liujit.upms.domain.params.SysUserRoleParams;
import com.liujit.upms.domain.query.SysUserQuery;
import com.liujit.upms.domain.result.SysUserResult;
import com.liujit.upms.domain.result.UserInfoResult;
import com.liujit.upms.service.SysUserService;
import com.liujit.validator.group.AddGroup;
import com.liujit.validator.group.ModifyGroup;
import com.liujit.web.utils.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description 系统用户管理接口
 * @Author liujun
 * @Date 2021-07-02 15:01:25
 */
@RequiredArgsConstructor
@Api(tags = "系统用户管理接口")
@RestController
@RequestMapping("/sys/user")
public class SysUserController {

    private final SysUserService sysUserService;

    @ApiOperation(value = "分页查询", notes = "根据系统用户分页查询条件，分页查询系统用户列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "query", value = "系统用户分页查询条件", dataType = "SysUserQuery", dataTypeClass = SysUserQuery.class, paramType = "query") })
    @GetMapping("/getPage")
    public PageBean<SysUserResult> getPage(SysUserQuery query) {
        return sysUserService.getPage(query);
    }

    @ApiOperation(value = "添加", notes = "根据系统用户添加请求参数，添加系统用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "params", value = "系统用户添加请求参数", dataType = "SysUserParams", dataTypeClass = SysUserParams.class, paramType = "body") })
    @PostMapping("add")
    public String add(@RequestBody @Validated(AddGroup.class) SysUserParams params) {
        sysUserService.save(params);
        return GlobalConstant.defPwd;
    }

    @ApiOperation(value = "修改", notes = "根据系统用户修改请求参数，修改系统用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "params", value = "系统用户修改请求参数", dataType = "SysUserParams", dataTypeClass = SysUserParams.class, paramType = "body") })
    @PostMapping("/modify")
    public Boolean modify(@RequestBody @Validated(ModifyGroup.class) SysUserParams params) {
        return sysUserService.save(params);
    }

    @ApiOperation(value = "查询", notes = "根据系统用户id，查询系统用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "系统用户id", dataType = "Long", dataTypeClass = Long.class, paramType = "query") })
    @GetMapping("/get")
    public SysUserResult get(Long id) {
        return sysUserService.get(id);
    }

    @ApiOperation(value = "查询详情", notes = "根据系统用户id，查询系统用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "系统用户id", dataType = "Long", dataTypeClass = Long.class, paramType = "query") })
    @GetMapping("/getDetail")
    public SysUserResult getDetail(Long id) {
        return sysUserService.getDetail(id);
    }

    @ApiOperation(value = "删除", notes = "根据系统用户删除请求参数，删除系统用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "params", value = "系统用户删除请求参数", dataType = "BaseParams", dataTypeClass = BaseParams.class, paramType = "body") })
    @PostMapping("/del")
    public Boolean del(@RequestBody BaseParams params) {
        return sysUserService.del(params);
    }

    @ApiOperation(value = "启用", notes = "根据系统用户启用请求参数，启用系统用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "params", value = "系统用户启用请求参数", dataType = "BaseParams", dataTypeClass = BaseParams.class, paramType = "body") })
    @PostMapping("/enabled")
    public Boolean enabled(@RequestBody BaseParams params) {
        return sysUserService.enabled(params);
    }

    @ApiOperation(value = "禁用", notes = "根据系统用户禁用请求参数，禁用系统用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "params", value = "系统用户禁用请求参数", dataType = "BaseParams", dataTypeClass = BaseParams.class, paramType = "body") })
    @PostMapping("/disabled")
    public Boolean disabled(@RequestBody BaseParams params) {
        return sysUserService.disabled(params);
    }

    @ApiOperation(value = "根据账号查询", notes = "根据系统用户账号，查询系统用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "account", value = "系统用户账号", dataType = "String", dataTypeClass = String.class, paramType = "query") })
    @GetMapping("/getByAccount")
    public SysUser getByAccount(String account) {
        return sysUserService.getByAccount(account);
    }

    @ApiOperation(value = "根据账号查询条数", notes = "根据系统用户账号，查询系统用户条数")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "account", value = "系统用户账号", dataType = "String", dataTypeClass = String.class, paramType = "query") })
    @GetMapping("/getCountByAccount")
    public Integer getCountByAccount(String account) {
        return sysUserService.getCountByAccount(account);
    }

    @ApiOperation(value = "校验密码", notes = "根据校验密码请求参数、验证密码是否正确")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "params", value = "校验密码请求参数", dataType = "CheckPasswordParams", dataTypeClass = CheckPwdParams.class, paramType = "body") })
    @PostMapping("/checkPwd")
    public Boolean checkPassword(@RequestBody @Validated CheckPwdParams params) {
        return sysUserService.checkPassword(params);
    }

    @ApiOperation(value = "修改密码", notes = "根据修改密码请求参数、修改密码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "params", value = "修改密码请求参数", dataType = "ModifyPasswordParams", dataTypeClass = ModifyPwdParams.class, paramType = "body") })
    @PostMapping("/modifyPwd")
    public Boolean modifyPassword(@RequestBody @Validated ModifyPwdParams params) {
        return sysUserService.modifyPassword(params);
    }

    @ApiOperation(value = "修改基础信息", notes = "根据系统用户修改请求参数，修改系统用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "params", value = "系统用户修改请求参数", dataType = "SysUserParams", dataTypeClass = SysUserParams.class, paramType = "body") })
    @PostMapping("/modifyInfo")
    public Boolean modifyInfo(@RequestBody @Validated SysUserParams params) {
        params.setId(JwtUtil.getUserId());
        return sysUserService.save(params);
    }

    @ApiOperation(value = "查询用户详细信息", notes = "查询用户详细信息，包含路由，权限")
    @GetMapping("/getInfo")
    public UserInfoResult getInfo() {
        return sysUserService.getInfo();
    }

    @ApiOperation(value = "查询系统用户角色id列表", notes = "根据系统用户id查询系统用户角色id列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "系统用户id", dataType = "Long", dataTypeClass = Long.class, paramType = "query") })
    @GetMapping("/getRoleIdByUserId")
    public List<Long> getRoleIdByUserId(Long userId) {
        return sysUserService.getRoleIdByUserId(userId);
    }

    @ApiOperation(value = "修改用户角色", notes = "根据系统用户角色、请求参数修改系统用户角色")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "params", value = "系统用户角色修改请求参数", dataType = "SysUserRoleParams", dataTypeClass = SysUserRoleParams.class, paramType = "body") })
    @PostMapping("/modifyUserRole")
    @Transactional(rollbackFor = Exception.class)
    public Boolean modify(@RequestBody SysUserRoleParams params) {
        return sysUserService.modifyUserRole(params);
    }
}
